package com.ahstu.learning.ui;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.ahstu.learning.R;
import com.ahstu.learning.base.BaseActivity;
import com.ahstu.learning.event.FinishEvent;
import com.ahstu.learning.friend.FriendTable;
import com.ahstu.learning.model.BaseModel;
import com.ahstu.learning.model.UserModel;

import org.greenrobot.eventbus.EventBus;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.SaveListener;

public class RegisterActivity extends BaseActivity implements OnClickListener {

    ImageView register_title_back;
    EditText register_username;
    EditText register_password;
    EditText register_password_again;
    Button register_button;
    ProgressDialog progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        register_title_back = (ImageView) findViewById(R.id.register_title_back);
        register_username = (EditText) findViewById(R.id.register_username);
        register_password = (EditText) findViewById(R.id.register_password);
        register_password_again = (EditText) findViewById(R.id.register_password_again);
        register_button = (Button) findViewById(R.id.register_button);
        register_title_back.setOnClickListener(this);
        register_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.register_title_back:
                finish();
                break;
            case R.id.register_button:
                UserModel.getInstance().register(register_username.getText().toString(),
                        register_password.getText().toString(),
                        register_password_again.getText().toString(),
                        new LogInListener() {
                            @Override
                            public void done(Object o, BmobException e) {
                                progress = new ProgressDialog(RegisterActivity.this);
                                progress.setMessage("注册中，请稍后。。。");
                                progress.setCanceledOnTouchOutside(false);
                                progress.show();

                                if (e == null) {
                                    EventBus.getDefault().post(new FinishEvent());

                                    //添加好友功能实现成功
                                    FriendTable friendTableMy = new FriendTable();
                                    FriendTable friendTableFriend = new FriendTable();
//                                    User userMy = UserModel.getInstance().getCurrentUser();

                                    friendTableMy.setMyName(register_username.getText().toString());
                                    friendTableMy.setFriendName("客服");

                                    friendTableFriend.setMyName("客服");
                                    friendTableFriend.setFriendName(register_username.getText().toString());
                                    //更新我的好友列表
                                    friendTableMy.save(RegisterActivity.this, new SaveListener() {
                                        @Override
                                        public void onSuccess() {
//                                            Toast.makeText(RegisterActivity.this, "添加成功", Toast.LENGTH_SHORT).show();

                                        }

                                        @Override
                                        public void onFailure(int i, String s) {
//                                            Toast.makeText(getContext(), "添加失败", Toast.LENGTH_SHORT).show();
                                        }
                                    });

                                    //更新被加的账号的好友列表
                                    friendTableFriend.save(RegisterActivity.this, new SaveListener() {
                                        @Override
                                        public void onSuccess() {
//                                            Toast.makeText(getContext(), "添加成功", Toast.LENGTH_SHORT).show();

                                        }

                                        @Override
                                        public void onFailure(int i, String s) {
//                                            Toast.makeText(getContext(), "添加失败", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                    SharedPreferences sp = getSharedPreferences("name", MODE_PRIVATE);
                                    SharedPreferences.Editor editor = sp.edit();
                                    editor.putString("username", register_username.getText().toString());
                                    editor.commit();
                                    startActivity(MainActivity.class, null, true);

                                } else {

                                    if (e.getErrorCode() == 202) {
                                        progress.dismiss();
                                        toast("用户名已存在，请重新输入");
                                        register_username.setText("");
                                    } else if (e.getErrorCode() == BaseModel.CODE_NOT_EQUAL) {
                                        progress.dismiss();
                                        toast("两次输入的密码不一致，请重新输入");
                                        register_password_again.setText("");
                                    } else {
                                        progress.dismiss();
                                        toast(e.getMessage());
                                    }
                                }
                            }
                        });
                break;
        }

    }


}
